﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WebBrowserTest.AnalyzeResults;

namespace WebBrowserTest.Analyzers
{
    public class LinkAnalyzer : AbstractAnalyzer
    {
        private string currentDomain;
        public LinkAnalyzer()
        {
            
        }

        public override AbstractAnalyzeResult GetAnalyseResult(string html, string url)
        {
            currentDomain = url;

            string outerDomainExpr = string.Format(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*", currentDomain);
            var outerMatches = Regex.Matches(html, outerDomainExpr);
            List<string> localDomain = new List<string>();
            List<string> outerDomain = new List<string>();
            for (int i = 0; i < outerMatches.Count; i++)
            {
                if(outerMatches[i].Groups[0].Value.Contains(currentDomain))
                {
                    localDomain.Add(outerMatches[i].Groups[0].Value);
                }
                else
                {
                    outerDomain.Add(outerMatches[i].Groups[0].Value);
                }
            }
            return new LinkAnalyzeResult
            {
                Date = DateTime.Now,
                DomainName = currentDomain,
                LocalDomainCount = localDomain.Count,
                OuterDomainCount = outerDomain.Count
            };

        }
    }
}
